package webone.registered.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.session.SqlSession;

import webone.javabean.Authority;
import webone.usersDAO.EmpDAOMapper;
import webone.usersDAO.WebOneSqlSession;

@WebServlet("/updateServlet")
public class UpdateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Authority au;
       
    public UpdateServlet() {
        super();
        
    }

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String[] ut=request.getParameterValues("aut");
		au=new Authority();
		String username=request.getParameter("username");
		for(int i=0;i<ut.length;i++) {
			setAuthority(ut[i]);
		}
		SqlSession session=WebOneSqlSession.getSqlSession();
		EmpDAOMapper empM=session.getMapper(EmpDAOMapper.class);
		empM.updateByInfo(username, au);
		session.commit();
		request.getRequestDispatcher("/queryServlet?username="+username).forward(request, response);
	}
	public void setAuthority(String aut) {
		switch(aut) {
		case "a1":
			au.setArticle1(true);
			break;
		case "a2":
			au.setArticle2(true);
			break;
		case "a3":
			au.setArticle3(true);
			break;
		case "a4":
			au.setArticle4(true);
			break;		
		}
	}

}
